<%@page contentType="text/html;charset=UTF-8" import="java.util.*" %>

<%@include file="common.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
  <base href="${ctx}">
  <link href="${ctx}/resources/wssh/bootstrap.min.css" rel="stylesheet"/>
  <link href="${ctx}/resources/wssh/style.css" rel="stylesheet"/>

  <script src="${ctx}/resources/wssh/jquery.min.js"></script>
  <script src="${ctx}/resources/wssh/term.js"></script>

  <title>${ip}-SHELL</title>
  <style type="text/css">
    .terminal{
      margin: 1px;
      font-family: courier,fixed,swiss,monospace,sans-serif;
      font-size: 14px;
      color: #f0f0f0;
      background: #000000;
      width: 98%;
      height: 98%;
    }
    body{
      padding: 1px;
    }
    table{
      width: 99%;
      height: 98%;
    }
  </style>
</head>

<body>

</body>

<script type="text/javascript">

  function openTerminal() {
    var websocket = null;

    var term = new Terminal(80, 53, function(key) {
      websocket.send(key);
    });

    term.open(document.body);

    //判断当前浏览器是否支持WebSocket
    if('WebSocket' in window){
      if('${ctx}'==''){
        websocket = new WebSocket("ws://"+window.location.host+"/openShell?type=${type}&ip=${ip}");
      }else{
        websocket = new WebSocket("ws://"+window.location.host+"/${ctx}/openShell?type=${type}&ip=${ip}");
      }
    }else{
      alert('Not support websocket')
    }


    //连接发生错误的回调方法
    websocket.onerror = function(){
      term.write("error");
    };

    //连接成功建立的回调方法
    websocket.onopen = function(event){
      //websocket.send("root\n\r");
    }

    //接收到消息的回调方法
    websocket.onmessage = function(){
      term.write(event.data);
    }

    //连接关闭的回调方法
    websocket.onclose = function(){
      term.write("close");
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function(){
      websocket.close();
    }

  }

  openTerminal();

</script>
</html>
